//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2007-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package com.openbravo.pos.inventory;

import java.awt.Component;
import com.openbravo.pos.base.AppLocal;
import java.util.List;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.JMessageDialog;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.pos.base.AppView;

import cn.ekuma.data.dao.bean.I_Category;
import cn.ekuma.data.ui.swing.AbstractParentDAOJEditor;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import com.openbravo.pos.bean.I_CategoryExt;
import com.openbravo.pos.panels.JCategoryTreeDialog;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import java.awt.BorderLayout;
import java.awt.Dimension;

/**
 *
 * @author adrianromero
 */
@SuppressWarnings("serial")
public abstract class CategoriesEditor<T extends I_CategoryExt> extends AbstractParentDAOJEditor<T,String> {
       
    private ComboBoxValModel m_CategoryModel;
    
    private Object m_id;
    I_DataLogicSales dlSales;

	private JTabbedPane tabbedPane;
    
    /** Creates new form JPanelCategories */
    public CategoriesEditor(AppView app, DirtyManager dirty,boolean showButtom,I_DataLogicSales dlSales) {
        super(dirty);
        this.dlSales = dlSales;
             
        initComponents();
        m_CategoryModel = new ComboBoxValModel();

        m_jCatalogAdd.setVisible(showButtom);
        m_jCatalogDelete.setVisible(showButtom);

        m_jName.getDocument().addDocumentListener(dirty);
        m_jCategory.addActionListener(dirty);
        m_jImage.addPropertyChangeListener("image", dirty);
        jHot.addActionListener(dirty);
        addChildTabPanel(tabbedPane);
    }
    
   abstract void addChildTabPanel(JTabbedPane tabbedPane);
    
    
    public void refresh() {      
        List a;       
        try {
            a = dlSales.list(getEditClass());
        } catch (BasicException eD) {
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.cannotloadlists"), eD);
            msg.show(this);
            a = new ArrayList();
        } 
        a.add(0, null); // The null item
        m_CategoryModel = new ComboBoxValModel(a);
        m_jCategory.setModel(m_CategoryModel);
    }
    
    @Override
	public void rebuild(T obj) throws BasicException {
		obj.setName(m_jName.getText());
		obj.setM_sParentID((String) m_CategoryModel.getSelectedKey());
		obj.setImage(ImageUtils.writeImage(m_jImage.getImage()));
		obj.setHot(jHot.isSelected());
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		m_jName.setText(obj.getName());
        m_CategoryModel.setSelectedKey(obj.getM_sParentID());
        m_jImage.setImage(ImageUtils.readImage(obj.getImage()));
        jHot.setSelected(obj.isHot());
        jHot.setEnabled(editAble);
        m_jName.setEnabled(editAble);
        m_jCategory.setEnabled(editAble);
        m_jImage.setEnabled(editAble);
        if(state==this.STATE_Edit){
          m_jCatalogDelete.setEnabled(editAble);
          m_jCatalogAdd.setEnabled(editAble);
        }else{
        	m_jCatalogDelete.setEnabled(false);
            m_jCatalogAdd.setEnabled(false);
        }
        jSearchCategory.setEnabled(editAble);
	}

	public Component getComponent() {
        return this;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
                                                        setLayout(new BorderLayout(0, 0));
                                                        
                                                        panel_1 = new JPanel();
                                                        panel_1.setPreferredSize(new Dimension(10, 30));
                                                        add(panel_1, BorderLayout.NORTH);
                                                                panel_1.setLayout(null);
                                                        
                                                                jLabel2 = new javax.swing.JLabel();
                                                                jLabel2.setBounds(0, 7, 103, 15);
                                                                panel_1.add(jLabel2);
                                                                
                                                                        jLabel2.setText(AppLocal.getIntString("Label.Name"));
                                                                        m_jName = new javax.swing.JTextField();
                                                                        m_jName.setBounds(102, 4, 249, 21);
                                                                        panel_1.add(m_jName);
                                                                        
                                                                        tabbedPane = new JTabbedPane(JTabbedPane.TOP);
                                                                        add(tabbedPane);
                                                                        
                                                                        JPanel panel = new JPanel();
                                                                        tabbedPane.addTab(AppLocal.getIntString("Label.baseDefine"), null, panel, null);
                                                                        panel.setLayout(null);
                                                                        jLabel3 = new javax.swing.JLabel();
                                                                        jLabel3.setBounds(10, 62, 80, 15);
                                                                        panel.add(jLabel3);
                                                                        
                                                                                jLabel3.setText(AppLocal.getIntString("label.image"));
                                                                                m_jImage = new com.openbravo.data.gui.JImageEditor();
                                                                                m_jImage.setBounds(49, 95, 260, 194);
                                                                                panel.add(m_jImage);
                                                                                m_jCatalogAdd = new javax.swing.JButton();
                                                                                m_jCatalogAdd.setBounds(366, 21, 170, 23);
                                                                                panel.add(m_jCatalogAdd);
                                                                                
                                                                                        m_jCatalogAdd.setText(AppLocal.getIntString("button.catalogadd")); // NOI18N
                                                                                        m_jCatalogDelete = new javax.swing.JButton();
                                                                                        m_jCatalogDelete.setBounds(366, 54, 170, 23);
                                                                                        panel.add(m_jCatalogDelete);
                                                                                        
                                                                                                m_jCatalogDelete.setText(AppLocal.getIntString("button.catalogdel")); // NOI18N
                                                                                                jLabel5 = new javax.swing.JLabel();
                                                                                                jLabel5.setBounds(10, 24, 90, 15);
                                                                                                panel.add(jLabel5);
                                                                                                
                                                                                                        jLabel5.setText(AppLocal.getIntString("label.prodcategory"));
                                                                                                        m_jCategory = new javax.swing.JComboBox();
                                                                                                        m_jCategory.setBounds(104, 21, 140, 20);
                                                                                                        panel.add(m_jCategory);
                                                                                                        jHot = new javax.swing.JCheckBox();
                                                                                                        jHot.setBounds(366, 101, 120, 23);
                                                                                                        panel.add(jHot);
                                                                                                        
                                                                                                                jHot.setText(AppLocal.getIntString("label.categoryPanel.ishot"));
                                                                                                                jSearchCategory = new javax.swing.JButton();
                                                                                                                jSearchCategory.setBounds(248, 21, 30, 20);
                                                                                                                panel.add(jSearchCategory);
                                                                                                                
                                                                                                                        jSearchCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
                                                                                                                        jSearchCategory.setFocusable(false);
                                                                                                                        jSearchCategory.setPreferredSize(new java.awt.Dimension(35, 25));
                                                                                                                        jSearchCategory.setRequestFocusEnabled(false);
                                                        jSearchCategory.addActionListener(new java.awt.event.ActionListener() {
                                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                                                jSearchCategoryActionPerformed(evt);
                                                            }
                                                        });
                                m_jCatalogDelete.addActionListener(new java.awt.event.ActionListener() {
                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                                        m_jCatalogDeleteActionPerformed(evt);
                                    }
                                });
                        m_jCatalogAdd.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(java.awt.event.ActionEvent evt) {
                                m_jCatalogAddActionPerformed(evt);
                            }
                        });
    }// </editor-fold>//GEN-END:initComponents

    private void m_jCatalogDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jCatalogDeleteActionPerformed
        try {
             dlSales.delCatalogProductCategory((String)m_id);
        } catch (BasicException e) {
            JMessageDialog.showMessage(this, new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotexecute"), e));
        }
        
    }//GEN-LAST:event_m_jCatalogDeleteActionPerformed

    private void m_jCatalogAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_jCatalogAddActionPerformed

        try {
            dlSales.delCatalogProductCategory((String)m_id);
            dlSales.addCatalogProductCategory((String)m_id); // y luego insertamos lo que queda
        } catch (BasicException e) {
            JMessageDialog.showMessage(this, new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotexecute"), e));
        }

    }//GEN-LAST:event_m_jCatalogAddActionPerformed

    private void jSearchCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jSearchCategoryActionPerformed
        JCategoryTreeDialog dailog=JCategoryTreeDialog.newJDialog(this);
        dailog.showDialog(m_CategoryModel.getDatas(),(I_Category) m_CategoryModel.getSelectedItem());
        if(dailog.isOK())
            m_CategoryModel.setSelectedItem(dailog.getSelected());
}//GEN-LAST:event_jSearchCategoryActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jHot;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JButton jSearchCategory;
    private javax.swing.JButton m_jCatalogAdd;
    private javax.swing.JButton m_jCatalogDelete;
    private javax.swing.JComboBox m_jCategory;
    private com.openbravo.data.gui.JImageEditor m_jImage;
    private javax.swing.JTextField m_jName;
    private JPanel panel_1;
    // End of variables declaration//GEN-END:variables

    @Override
	public void activate() throws BasicException {	
	}
}
